package com.network.retrofit_rx.Api;

import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * token 添加到头拦截器
 */
public class TokenHeadInterceptor implements Interceptor {
    private String token;

    public TokenHeadInterceptor(String token) {
        this.token = token;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();  //获取请求
        Request tokenRequest = null;
        if (TextUtils.isEmpty(token)) {
            return chain.proceed(originalRequest);
        }
        tokenRequest = originalRequest.newBuilder().header("Authorization", token).build();
        return chain.proceed(tokenRequest);
    }
}
